package com.kongchengji.zhanguafou.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kongchengji.zhanguafou.constant.WxInfo;
import com.kongchengji.zhanguafou.domain.QuartzDO;
import com.kongchengji.zhanguafou.domain.Result;
import com.kongchengji.zhanguafou.service.QuartzService;
import com.kongchengji.zhanguafou.utils.InfoByCodeUtil;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

//查询任务详情控制类
@Controller
@RequestMapping("/quartz")
public class QuartzController {
    @Autowired
    private QuartzService quartzService;
    private String openId;
    Logger logger = LoggerFactory.getLogger(ICPController.class);

    /**
     * 查看某个用户某个分组下的所有任务
     * @param jobGroup 任务分组
     * @return
     */
    @RequestMapping("/getListByGroup")
    @ResponseBody
    public String getListByGroup(String jobGroup,HttpServletResponse response) {
        try {
            //将任务的url或域名为键,是否运行为值。
            Map<String,String> map = quartzService.checkJobRun(jobGroup,WxInfo.openId);
            response.setHeader("Cache-Control","no-cache");
//            JSONObject jsonObject = new JSONObject();
//            JSONObject itemJSONObj = JSONObject.parseObject(JSONObject.toJSONString(map));
//            jsonObject.put("openId",openId);
//            jsonObject.put("quartz",itemJSONObj);
            return JSONObject.toJSONString(map);
        } catch (SchedulerException e) {
            logger.error("查询某个用户分组计划任务时出现异常");
            //如果出错直接返回
            return "";
        }
    }

    /**
     * 打开监控详情页
     * @return
     */
    @RequestMapping("/openMonitor_detail")
    public String openMonitor_detail(String code, Model model) {
        WxInfo.openId = InfoByCodeUtil.getOpenId(code);
        //默认查询该用户服务器监控组的任务
        try {
            Map<String,String> serverMap = quartzService.checkJobRun("Ping",WxInfo.openId);
            model.addAttribute("serverMap",serverMap);
        } catch (SchedulerException e) {
            //先不对异常做处理
            e.printStackTrace();
        }
        return "monitor_detail";
    }

    /**
     * 开启监控任务
     * @param groupName 分组前缀加监控的url或域名
     */
    @RequestMapping("/turnOnTask")
    @ResponseBody
    public void turnOnTask(String groupName,HttpServletResponse response) {
        //组装任务唯一标识
        String jobName = groupName + WxInfo.openId;
        response.setHeader("Cache-Control","no-cache");
        quartzService.turnOnTask(jobName);
    }

    /**
     * 删除监控任务
     * @param groupName 分组前缀加监控的url或域名
     */
    @RequestMapping("/turnDownTask")
    @ResponseBody
    public void turnDownTask(String groupName,HttpServletResponse response) {
        String jobName = groupName + WxInfo.openId;
        response.setHeader("Cache-Control","no-cache");
        quartzService.turnDownTask(jobName);
    }

    /**
     * 保存编辑修改后的任务
     * @param quartzDO
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Result editIcpTask(QuartzDO quartzDO,@RequestParam("interval") String interval) {
        quartzDO.setCronExpression("0 0/" + interval + " * * * ?");
        quartzDO.setJobName(quartzDO.getJobGroup() + quartzDO.getUrl() + WxInfo.openId);
        try {
            return quartzService.editQuartzDO(quartzDO);
        } catch (SchedulerException e) {
            return Result.error("修改配置失败");
        }
    }
}
